package TAIC.Classifier;

import java.io.File;
import java.util.Scanner;

public class DictAidedBayes extends Bayes {
	public String imageModelFile ; 
	public Model imageModel = new Model ( MaxClass, ImageMaxWords ) ;
	
	public DictAidedBayes ()  throws Exception {
		super () ;
		this.imageModelFile = "imageModel.txt" ; 
		initImageModel();
	}
	
	public DictAidedBayes ( String imageModelFile ) throws Exception {
		super () ;
		this.imageModelFile = imageModelFile; 
		initImageModel();
	}
	
	public void initImageModel () throws Exception {
		Scanner scanner = new Scanner ( new File ( imageModelFile ) ) ;
		scanner.next() ; scanner.next() ;
		for ( int i = 0 ;i < ImageMaxWords ; i ++ ) imageModel.wc [ i ] = new double [ MaxClass ]; 
		for ( int i = 0 ; i < MaxClass ; i ++ ) {  
			for ( int j = 0 ; j < ImageMaxWords ; j ++ ) 
				imageModel.wc [j][i] = Math.log ( scanner.nextDouble() + 0.00000001) ;
			imageModel.c [ i ] = Math.log( 1.0 / MaxClass );
		}
	}
	
	public void train ( String trainFile ){
		super.train ( trainFile ) ;
		//System.out.println ( "asfaf " + lambda  ) ;
		for ( int j = 0 ; j < ImageMaxWords ; j ++ ) {
			if ( model.wc [ j ] == null ) model.wc[ j ] = new double [ MaxClass ] ;
			for ( int i = 0 ; i < MaxClass ; i ++ ) {
				model.wc [ j ][ i ] += model.wc[j][i] * lambda / ( 1 + lambda ) + imageModel.wc[j][i] / ( 1 + lambda ) ;
//				if ( j < 10 ) System.out.println ( model.wc[j][i] ) ; 
			}
		}
	}
	
	public boolean isPipe () {
		return true ;
	}
	
}